package com.dingding.service.user.core.dao.user.impl;

import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.beans.Oauthinfo;
import com.dingding.common.core.beans.UserLogin;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.user.dto.adduser.AddUserAttribute;
import com.dingding.service.user.core.dao.IOauthinfoDao;
import com.dingding.service.user.core.dao.IUserAccountDao;
import com.dingding.service.user.core.dao.IUserDetailDao;
import com.dingding.service.user.core.dao.IUserIntegralDao;
import com.dingding.service.user.core.dao.IUserLoginDao;
import com.dingding.service.user.core.dao.IVehicleInfoDao;
import com.dingding.service.user.core.dao.user.IUserOperateDao;
import com.dingding.service.user.core.enums.OauthNameEnum;

@Component("userOperateDao")
public class UserOperateDaoImpl implements IUserOperateDao{
	
	private Logger log = Logger.getLogger(this.getClass());

	@Resource
	private ICriteriaBaseDao criteriaBaseDao;
	@Resource
	protected IUserDetailDao userDetailDao;
	@Resource
	protected IUserLoginDao userLoginDao;
	@Resource
	protected IUserAccountDao userAccountDao;
	@Resource
	protected IUserIntegralDao userIntegralDao;
	@Resource
	protected IVehicleInfoDao vehicleinfoDao;
	@Resource
	protected IOauthinfoDao oauthinfoDao;
	
	@Override
	@Transactional(readOnly = true)
	public UserLogin getByTelephone(String tele) {
		Criterion criterion = Restrictions.eq("userName", tele);
		try {
			List<UserLogin> uls = this.criteriaBaseDao.queryCriteriaList(UserLogin.class, criterion);
			if(uls == null || uls.size() == 0){
				log.info("查询不到用户，用户电话是:" + tele);
				return null;
			}else if(uls.size() > 1){
				throw new RuntimeException("userlogin表里存在两条以上的记录，改手机号是：" + tele);
			}else{
				return uls.get(0);
			}
		} catch (Exception e) {
			log.error("查询到用户出现异常！UserOperateDaoImpl ：getByTelephone()",e);
			return null;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public UserLogin addUser(AddUserAttribute addUserAttribute) {
		String tellphone = addUserAttribute.getTelephone();
		String userLoginId = ToolUtil.getUid();
		UserLogin ul = null;
		try {
			ul = this.userLoginDao.addUserlogin(tellphone, userLoginId,
					"");														// 添加用户登录信息
			this.userDetailDao.addUserDetail(tellphone, userLoginId);			// 添加用户详细信息
			this.userAccountDao.addUserAccount(userLoginId);					// 添加用户账户信息
			this.userIntegralDao.addUserIntegral(userLoginId);					// 添加用户积分信息
			this.vehicleinfoDao.addVehicleInfo(userLoginId);					// 添加用户车辆信息
			this.oauthinfoDao.save(
					new Oauthinfo(userLoginId, OauthNameEnum.WECHAT.getName()));// 添加注册来源信息
			return ul;
		} catch (Exception e) {
			log.error("初始化用户出现异常!UserOperateDaoImpl:addUser()",e);
			return null;
		}
	}
}
